<?php
/**
 * @title 2280. 表示一个折线图的最少线段数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $stockPrices
     * @return Integer
     */
    function minimumLines($stockPrices) {
        /**
         * @since 2024-05-17 股价的数量
         * @since 2024-05-17 返回要表示一个折线图所需要的 最少线段数
         * @since 2024-05-20 股价按照日期升序排列
         */
        $n = count($stockPrices);
        if($n == 1) return 0;
        $min = 1;
        sort($stockPrices);

        /**
         * @since 2024-05-17 上一个折现起点
         * @since 2024-05-17 计算斜率
         */
        $s = 0;
        $x1 = $stockPrices[1][1] - $stockPrices[0][1];
        $y1 = $stockPrices[1][0] - $stockPrices[0][0];
        for($i=2; $i<$n; $i++){
            /**
             * @since 2024-05-17 所有 day 互不相同
             * @since 2024-05-17 [day,price]，除数不会是0
             */
            $x2 = $stockPrices[$i][1] - $stockPrices[$i-1][1];
            $y2 = $stockPrices[$i][0] - $stockPrices[$i-1][0];

            /**
             * @since 2024-05-20 php精度问题，[[1,1],[500000000,499999999],[1000000000,999999998]]
             * @since 2024-05-20 499999998/499999999 == 499999999/500000000，返回true
             * @since 2024-05-20 借助 BCMath函数库 可处理
             */
            // if($x1/$y1 != $x2/$y2){
            //     $min++;
            //     $s = $i-1;
            //     list($x1, $y1) = [$x2, $y2];
            // } else {}

            if($x1*$y2 != $x2*$y1){
                $min++;
                $s = $i-1;
                list($x1, $y1) = [$x2, $y2];
            } else {}
        }

        /**
         * @return
         */
        return $min;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.17_3.php
 */
$datas = [
    // [[[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]], // 3
    // [[[72,98],[62,27],[32,7],[71,4],[25,19],[91,30],[52,73],[10,9],[99,71],[47,22],[19,30],[80,63],[18,15],[48,17],[77,16],[46,27],[66,87],[55,84],[65,38],[30,9],[50,42],[100,60],[75,73],[98,53],[22,80],[41,61],[37,47],[95,8],[51,81],[78,79],[57,95]]], // 29
    [[[1,1],[500000000,499999999],[1000000000,999999998]]], // 2
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->minimumLines(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
